<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>随机评分 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/random-scoring.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/random-scoring.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/random-scoring.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/random-scoring.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="search-in-depth.html">深入搜索</a></span>
»
<span class="breadcrumb-link"><a href="controlling-relevance.html">控制相关度</a></span>
»
<span class="breadcrumb-node">随机评分</span>
</div>
<div class="navheader">
<span class="prev">
<a href="function-score-filters.html">« 过滤集提升权重</a>
</span>
<span class="next">
<a href="decay-functions.html">越近越好 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="random-scoring"></a>随机评分<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/170_Relevance/55_Random_scoring.asciidoc">edit</a>
</h2>
</div></div></div>
<p>你可能会想知道 <em>一致随机评分（consistently random scoring）</em> 是什么，又为什么会使用它。之前的例子是个很好的应用场景，前例中所有的结果都会返回 1 、 2 、 3 、 4 或 5 这样的最终评分 <code class="literal">_score</code> ，可能只有少数房子的评分是 5 分，而有大量房子的评分是 2 或 3 。</p>
<p>作为网站的所有者，总会希望让广告有更高的展现率。在当前查询下，有相同评分 <code class="literal">_score</code> 的文档会每次都以相同次序出现，为了提高展现率，在此引入一些随机性可能会是个好主意，这能保证有相同评分的文档都能有均等相似的展现机率。</p>
<p>我们想让每个用户看到不同的随机次序，但也同时希望如果是同一用户翻页浏览时，结果的相对次序能始终保持一致。这种行为被称为 <em>一致随机（consistently random）</em> 。</p>
<p><code class="literal">random_score</code> 函数会输出一个 0 到 1 之间的数，当种子 <code class="literal">seed</code> 值相同时，生成的随机结果是一致的，例如，将用户的会话 ID 作为 seed ：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /_search
{
  "query": {
    "function_score": {
      "filter": {
        "term": { "city": "Barcelona" }
      },
      "functions": [
        {
          "filter": { "term": { "features": "wifi" }},
          "weight": 1
        },
        {
          "filter": { "term": { "features": "garden" }},
          "weight": 1
        },
        {
          "filter": { "term": { "features": "pool" }},
          "weight": 2
        },
        {
          "random_score": { <a id="CO118-1"></a><i class="conum" data-value="1"></i>
            "seed":  "the users session id" <a id="CO118-2"></a><i class="conum" data-value="2"></i>
          }
        }
      ],
      "score_mode": "sum"
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO118-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">random_score</code> 语句没有任何过滤器 <code class="literal">filter</code> ，所以会被应用到所有文档。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO118-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>将用户的会话 ID 作为种子 <code class="literal">seed</code> ，让该用户的随机始终保持一致，相同的种子 <code class="literal">seed</code> 会产生相同的随机结果。</p>
</td>
</tr>
</table>
</div>
<p>当然，如果增加了与查询匹配的新文档，无论是否使用一致随机，其结果顺序都会发生变化。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="function-score-filters.html">« 过滤集提升权重</a>
</span>
<span class="next">
<a href="decay-functions.html">越近越好 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>